﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include"contact.h"
void SLInit(SL* ps) {
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
void SLCheckcapacity(SL* ps) {
	if (ps->capacity == ps->size)//说明空间不足，需要开辟空间
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newcapacity * sizeof(SLDateType));
		if (tmp == NULL)
		{
			perror("realloc fail!\n");
			return 1;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
}
void SLPushBack(SL* ps, SLDateType x) {
	assert(ps);
	SLCheckcapacity(ps);
	ps->a[ps->size++] = x;
}
void SLDestory(SL* ps)
{
	SLCheckcapacity(ps);
	if (ps->a)
	{
		free(ps->a);
	}
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}
void SLPushFront(SL* ps, SLDateType x) {
	SLCheckcapacity(ps);
	for (size_t i = ps->size; i > 0; i--)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
void SLPopback(SL* ps) {
	assert(ps);
	assert(!SLIsempty(ps));
	ps->size--;
}
void SLprint(SL* ps) {
	for (size_t i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void SLPopFront(SL* ps) {
	assert(ps);
	assert(!SLIsempty(ps));
	for (size_t i = 0; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i - 1];
	}
	ps->size--;
}
void SLInsert(SL* ps, int pos, SLDateType x) {
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckcapacity(ps);
	for (size_t i = ps->size; i > pos; i--)
	{
		ps->a[i] = ps->a[i-1];
	}
	ps->a[pos] = x;
	ps->size++;
	//有了在指定位置插入数据的函数，在头插尾插中直接调用即可
}
void SLErase(SL* ps, int pos) {
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	for (size_t i = pos; i < ps->size - 1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

//bool SLFind(SL* ps, char[] ) {
//	assert(ps);
//	for (size_t i = 0; i < ps->size; i++)
//	{
//		if (ps->a[i].name == x)
//		{
//			return true;
//		}
//	}
//	return false;
//}

bool SLIsempty(SL* ps) {
	assert(ps);
	return ps->size == 0;
}